package com.pwc.conversionTool.Manager;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletContext;

import org.dom4j.DocumentException;

import com.pwc.conversionTool.config.ConfigFileInfo;
import com.pwc.conversionTool.config.XMLConfig;

public class XMLConfigImpl extends XMLConfig {

	private static ServletContext context;
	
	public static XMLConfigImpl initial(ServletContext context)throws DocumentException
	{
		XMLConfigImpl.context=context;
		String path=XMLConfigImpl. context.getRealPath("")+File.separator+ConfigFileInfo.fileNameString;
		return new XMLConfigImpl(path);
	}

	private XMLConfigImpl(String path) throws DocumentException {
		super(path);
	}

	public String getEndpoint() {
		return configBase.getSingleStringValue("//soap/endpoint");
	}

	public void setEndpoint(String endpoint) throws IOException {
		configBase.setSingleStringValue("//soap/endpoint", endpoint);
	}

	public String getUserName() {
		return configBase.getSingleStringValue("//soap/username");
	}

	public void setUserName(String userName) throws IOException {
		configBase.setSingleStringValue("//soap/username", userName);
	}

	public String getPassword() {
		return configBase.getSingleStringValue("//soap/password");
	}

	public void setPassword(String password) throws IOException {
		configBase.setSingleStringValue("//soap/password", password);
	}
	
	public String getPasswordType() {
		return configBase.getSingleStringValue("//soap/passwordType");
	}

	public void setPasswordType(String security) throws IOException {
		configBase.setSingleStringValue("//soap/passwordType", security);
	}
	

	
	public boolean addFolderNode(String key,String value) throws IOException
	{
		if(key.matches("^[A-Za-z][a-zA-Z_0-9]*$"))
		{
			addNodeByXpath("//application", key, value);
			saveDocument();
		}
		
		return key.matches("^[A-Za-z][a-zA-Z_0-9]*$");
	}
	
	public void deleteFolderNode(String key) throws IOException{
		deleteNodeByXpath("//application/"+key);
		saveDocument();
	}
	
	
	public Map<String, String> getXMLMap(String xPath){
		return configBase.getXMLMap(xPath);
	}
	


}
